﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using DataAccess.DBConnections;

namespace DataAccess.Connections
{
    class CystalReportEngine
    {
        #region Attributes

        private DBConnectionsEngine DBEngine = new DBConnectionsEngine();
		#endregion

		public void setRptDatabase(ReportDocument rpt)
		{
			ConnectionInfo cRpt = new ConnectionInfo();
			Database crDatabase; 
			CrystalDecisions.CrystalReports.Engine.Tables crTables; 
			CrystalDecisions.CrystalReports.Engine.Table crTable; 
			TableLogOnInfo crTableLogOnInfo;

            //cRpt.ServerName= DBEngine.ServerName;
            //cRpt.DatabaseName= string.Empty;
            //cRpt.UserID= DBEngine.UserID;
            //cRpt.Password= DBEngine.Password;

            cRpt.ServerName = "xxx";
			cRpt.DatabaseName= string.Empty;
            cRpt.UserID = "xxx";
            cRpt.Password = "xxx";

			crDatabase=rpt.Database;
			crTables=crDatabase.Tables;
	
			for (int i = 0; i < crTables.Count; i++)
			{ 
				crTable = crTables[i]; 
				crTableLogOnInfo = crTable.LogOnInfo; 
				crTableLogOnInfo.ConnectionInfo=cRpt; 
				crTable.ApplyLogOnInfo(crTableLogOnInfo); 
				crTable.Location=crTable.Location.Substring(crTable.Location.LastIndexOf(".") + 1);
			}
		}
        public void setRptDatabaseUnLocation(ReportDocument rpt)
        {
            ConnectionInfo cRpt = new ConnectionInfo();
            Database crDatabase;
            CrystalDecisions.CrystalReports.Engine.Tables crTables;
            CrystalDecisions.CrystalReports.Engine.Table crTable;
            TableLogOnInfo crTableLogOnInfo;

            //cRpt.ServerName = DBEngine.ServerName;
            //cRpt.DatabaseName = string.Empty;
            //cRpt.UserID = DBEngine.UserID;
            //cRpt.Password = DBEngine.Password;

            cRpt.ServerName = "xx";
            cRpt.DatabaseName = string.Empty;
            cRpt.UserID = "xxx";
            cRpt.Password = "xxx";

            crDatabase = rpt.Database;
            crTables = crDatabase.Tables;

            for (int i = 0; i < crTables.Count; i++)
            {
                crTable = crTables[i];
                crTableLogOnInfo = crTable.LogOnInfo;
                crTableLogOnInfo.ConnectionInfo = cRpt;
                crTable.ApplyLogOnInfo(crTableLogOnInfo);
            }
        }
    }
}
